#!/bin/bash
CURRENT_FILE=install.sh
printf "[%s][%010s:%3s] JDK 开始安装...\n" `date +"%T"` $CURRENT_FILE $LINENO
SOURCE_DIR=$PWD
# 1. Get install packet
for file in ${SOURCE_DIR}/*; do
	if [[ $file =~ jdk.*x64.*\.tar\.gz ]]; then
		JDK_PACK=$file
	fi
done
# 2. Check install packet
if [[ ! -f $JDK_PACK ]]; then
	printf "[%s][%010s:%3s] JDK 安装包不存在：\"%s\"\n" `date +"%T"` $CURRENT_FILE $LINENO $JDK_PACK
    exit
fi  
printf "[%s][%010s:%3s] JDK 安装包：%s\n" `date +"%T"` $CURRENT_FILE $LINENO $JDK_PACK
# 3. Uninstall JDK
for i in $(rpm -qa | grep jdk | grep -v grep)
do
	printf "[%s][%010s:%3s] 卸载已安装 JDK： \"%s\"\n" `date +"%T"` $CURRENT_FILE $LINENO $i
	rpm -e --allmatches $i --nodeps > /dev/null 2>&1
done
# 4. Install JDK
PRE_JAVA_HOME=/usr/local/java
rm -rf $PRE_JAVA_HOME
mkdir -p $PRE_JAVA_HOME
JDK_TOTAL_SIZE=`gzip -l $JDK_PACK | grep '%' | awk '{print $2}'`
JDK_BLOCK_SIZE=`expr $JDK_TOTAL_SIZE / 51200`
JDK_BLOCK_SIZE=`expr $JDK_BLOCK_SIZE + 1`
tar --blocking-factor=$JDK_BLOCK_SIZE --checkpoint=1 --checkpoint-action='ttyout=JDK 解压中: %u%\r' -zxf $JDK_PACK -C $PRE_JAVA_HOME/
printf "[%s][%010s:%3s] JDK 解压完成\n" `date +"%T"` $CURRENT_FILE $LINENO

for file in $PRE_JAVA_HOME/*; do
	if [[ $file =~ .*\..* ]]; then
		mv $PRE_JAVA_HOME/${file##*/} $PRE_JAVA_HOME/jdk
	fi
done
# 5. Config JAVA PATH
CONFIG_FILE=/etc/profile
if [[ -f /etc/profile.base ]]; then
	cp -f /etc/profile.base /etc/profile 
else
	cp -f /etc/profile /etc/profile.base
fi
echo "# Aaron for JAVA_HOME at `date +"%Y-%m-%d %H:%M:%S"`" >> $CONFIG_FILE 
echo "JAVA_HOME=$PRE_JAVA_HOME/jdk" >> $CONFIG_FILE
echo "JRE_HOME=$PRE_JAVA_HOME/jdk/jre" >> $CONFIG_FILE
echo "PATH=\$PATH:\$JAVA_HOME/bin:\$JRE_HOME/bin" >> $CONFIG_FILE 
echo "CLASSPATH=\$CLASSPATH:\$JAVA_HOME/lib/dt.jar:\$JAVA_HOME/lib/tools.jar:\$JRE_HOME/lib" >> $CONFIG_FILE 
echo "export JAVA_HOME JRE_HOME PATH CLASSPATH" >> $CONFIG_FILE
source $CONFIG_FILE
printf "[%s][%010s:%3s] 配置 JAVA PATH 完成\n" `date +"%T"` $CURRENT_FILE $LINENO
# 6. Config JMX
if [[ -f $PRE_JAVA_HOME/jdk/jre/lib/management/jmxremote.access.base ]]; then
	cp -f $PRE_JAVA_HOME/jdk/jre/lib/management/jmxremote.access.base $PRE_JAVA_HOME/jdk/jre/lib/management/jmxremote.access
else
	cp -f $PRE_JAVA_HOME/jdk/jre/lib/management/jmxremote.access $PRE_JAVA_HOME/jdk/jre/lib/management/jmxremote.access.base
fi
echo "" >> $PRE_JAVA_HOME/jdk/jre/lib/management/jmxremote.access
echo "etcpms        readwrite \\" >> $PRE_JAVA_HOME/jdk/jre/lib/management/jmxremote.access
echo "              create javax.management.monitor.*,javax.management.timer.* \\" >> $PRE_JAVA_HOME/jdk/jre/lib/management/jmxremote.access
echo "              unregister" >> $PRE_JAVA_HOME/jdk/jre/lib/management/jmxremote.access
cp -f $PRE_JAVA_HOME/jdk/jre/lib/management/jmxremote.password.template $PRE_JAVA_HOME/jdk/jre/lib/management/jmxremote.password
echo "" >> $PRE_JAVA_HOME/jdk/jre/lib/management/jmxremote.password
echo "etcpms passw0rd" >> $PRE_JAVA_HOME/jdk/jre/lib/management/jmxremote.password
chmod 0400 $PRE_JAVA_HOME/jdk/jre/lib/management/jmxremote.password
printf "[%s][%010s:%3s] 配置 JAVA JMX 完成\n" `date +"%T"` $CURRENT_FILE $LINENO
# 7. Reload system env
source /root/.bashrc
source /etc/profile
printf "[%s][%010s:%3s] JDK 安装完成\n" `date +"%T"` $CURRENT_FILE $LINENO
# 8. Check version
export GREP_COLOR='1;32'; java -version 2>&1 | grep -i "java.*" --color=auto